home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 009 / smartrm1.arc / CREATE.ASM next >
Encoding:
Assembly Source File  |  1985-12-27  |  10.5 KB  |  380 lines

  1.     page    60,132
  2. TITLE    CREATE -  BASIC subrtn to decode track 40 for SMARTERM 100 ver. 4.0c
  3. ;----------------------------------------------------------------------
  4. ;
  5. ;    This subroutine is called from BASIC using:
  6. ;
  7. ;    CALL CREATE(FILE$,ERROR%)
  8. ;
  9. ;    Passed:   FILE$ = Name of file to create. An ASCIIZ string
  10. ;                      with Drive, path, filename, and ending with
  11. ;                      a byte of zero.
  12. ;    Returned: ERROR%= Integer for the return of an error condition.
  13. ;                      The following values can be returned:
  14. ;                      00 - No error
  15. ;                      01 - Incorrect DOS version, must be higher than 2.00
  16. ;                      02 - File not found
  17. ;                      03 - Path not found
  18. ;                      04 - Too many open files
  19. ;                      05 - File access denied
  20. ;                      06 - Invalid handle
  21. ;                      07
  22. ;                      :  - Error reading sector number 1-9
  23. ;                      15
  24. ;                      16 - Correct number of bytes not read or written
  25. ;                           to file
  26. ;                      17 - Error resetting diskette system
  27. ;
  28. ;    by Keith M. Bailey         12/29/84       version 0.01
  29. ;
  30. ;----------------------------------------------------------------------
  31. DATASG    SEGMENT    PARA    'DATA'
  32. BUFFER    DB    0400H*9 DUP(?)        ;Buffer to store 9 decodes sectors
  33. FILE    DB    255 DUP(00)        ;Space to store filename
  34. HANDLE    DW    00            ;Store file handle
  35. DSKPRM    DB    0BH DUP(00)        ;Space for disk parameter table
  36. OFF_1E    DW    00            ;Store address of old disk
  37. SEG_1E    DW    00            ;   parameter table
  38. TRIES    DB    00            ;Number of tries at disk access
  39. DATASG    ENDS
  40. ;----------------------------------------------------------------------
  41.     PUBLIC CREATE, FMERGE
  42. CODESG    SEGMENT    PARA    'CODE'
  43.     ASSUME CS:CODESG,DS:NOTHING,ES:DATASG
  44. CREATE    PROC    FAR
  45.     PUSH    BP            ;Save BP
  46.     MOV    BP,SP
  47.     PUSH    ES            ;Save ES
  48.     MOV    AX,DATASG
  49.     MOV    ES,AX            ;Set ES = DATASG
  50.  
  51.     MOV    AX,00            ;Initialize with no error
  52.     CALL    ERROR
  53.  
  54.     MOV    AH,30H
  55.     INT    21H            ;Get DOS version
  56.     CMP    AL,02            ;Is it 2.00 or above
  57.     JAE    OK1            ;   Yes - continue
  58.     MOV    AX,01            ;   No  - set error condition
  59.     CALL    ERROR            ;         and leave
  60.     JMP    DONE
  61.  
  62. OK1:    CALL    GET_FILENAME
  63.  
  64.     PUSH    DS
  65.     MOV    AX,DATASG
  66.     MOV    DS,AX            ;Set DS = DATASG
  67.     MOV    DX,OFFSET FILE        ;DS:DX points to ASCIIZ string filename
  68.     MOV    CX,00            ;Normal file attribute
  69.     MOV    AH,3CH
  70.     INT    21H            ;Create file
  71.     JNC    OK2            ;If no error continue
  72.     POP    DS            ;Error - restore DS
  73.     CALL    ERROR            ;   set error condition
  74.     JMP    DONE            ;   and leave
  75.  
  76. OK2:    MOV    HANDLE,AX        ;Save file handle
  77.     CALL    NEW_PARMS        ;Get new disk parameter table
  78.     CALL    RESET            ;Reset diskette system
  79.     MOV    TRIES,00        ;Set number of tries to 0
  80.     MOV    DX,0000H        ;Drive A, head 00
  81.     MOV    CX,2801H        ;Track 40, sector 1
  82.     MOV    BX,OFFSET BUFFER    ;Point to BUFFER
  83.     MOV    AX,0201H        ;Read one sector
  84.  
  85. NXT:    INT    13H
  86.     JNC    SECT9            ;Error encountered ?
  87.     CMP    AH,10H            ;Was it bad CRC ?
  88.     JNE    ERR1            ;   No  - something's wrong
  89.     CMP    CL,09            ;Is it sector 9 ?
  90.     JE    ERR1            ;   Yes - something's wrong
  91.     INC    CL            ;Next sector
  92.     ADD    BX,0400H        ;Point along in BUFFER
  93.     MOV    AX,0201H        ;Read one sector
  94.     MOV    TRIES,00        ;Set number of tries to 0
  95.     JMP    NXT            ;Read next sector
  96.  
  97. ERR1:    INC    TRIES            ;Keep track of tries
  98.     CMP    TRIES,03        ;If more than 3 tries
  99.     JA    ERR2            ;   exit with error message
  100.     CALL    RESET            ;Reset diskette system
  101.     MOV    AX,0201            ;Read one sector
  102.     JMP    NXT            ;Try it again
  103.  
  104. ERR2:    MOV    AX,06            ;Base of error code
  105.     MOV    CH,00
  106.     ADD    AX,CX            ;Add number of sector being read
  107.     POP    DS            ;Restore DS
  108.     CALL    ERROR            ;Set error code
  109.     JMP    DONE            ;Exit
  110.  
  111. SECT9:    CMP    CL,09            ;Was the 9th sector read
  112.     JNE    ERR1            ;   No  - try it again
  113.  
  114.     CALL    RESTORE            ;Restore original disk parameters
  115.  
  116.     MOV    BX,HANDLE        ;File handle
  117.     MOV    CX,0400H*9        ;Number of bytes to write
  118.     MOV    DX,OFFSET BUFFER    ;DS:DX points to BUFFER
  119.     MOV    AH,40H
  120.     INT    21H            ;Write to file
  121.     JNC    OK3            ;Jump if no error
  122.     POP    DS            ;Restore DS
  123.     CALL    ERROR            ;Set error code
  124.     JMP    DONE            ;Exit
  125.  
  126. OK3:    CMP    AX,0400H*9        ;All bytes written ?
  127.     JE    OK4            ;   Yes - continue
  128.     POP    DS            ;Restore DS
  129.     MOV    AX,16
  130.     CALL    ERROR            ;Set error code
  131.     JMP    DONE            ;Exit
  132.     
  133. OK4:    MOV    BX,HANDLE
  134.     MOV    AH,3EH
  135.     INT    21H            ;Close file handle
  136.     POP    DS            ;Restore DS
  137.     JNC    DONE            ;If no error - DONE
  138.     CALL    ERROR            ;Set error code
  139.  
  140.  
  141. DONE:    POP    ES            ;Restore ES
  142.     POP    BP            ;Restore BP
  143.     RET    4            ;Return
  144.  
  145. CREATE    ENDP
  146.  
  147. ;----------------------------------------------------------------------
  148. ;
  149. ;    Subroutine to store error condition in ERROR%.
  150. ;
  151. ;    Passed: AX = error condition.
  152. ;            DS = must point to BASIC's data segment
  153. ;
  154. ;    Registers destroyed: none
  155. ;----------------------------------------------------------------------
  156. ERROR    PROC    NEAR
  157.     PUSH    SI
  158.     MOV    SI,6[BP]        ;Point to ERROR%
  159.     MOV    WORD PTR DS:[SI],AX    ;Move error code into ERROR%
  160.     POP    SI            ;Restore SI
  161.     CALL    RESTORE            ;Restore original disk parameters
  162.     RET                ;Return
  163. ERROR    ENDP
  164.  
  165. ;----------------------------------------------------------------------
  166. ;
  167. ;    Subroutine to store filename (1st parameter), in FILE.
  168. ;
  169. ;    Passed: AX = error condition.
  170. ;            DS = must point to BASIC's data segment
  171. ;
  172. ;    Registers destroyed: CX,DI,SI
  173. ;----------------------------------------------------------------------
  174. GET_FILENAME    PROC    NEAR
  175.     MOV    DI,8[BP]        ;Point to string descriptor
  176.     MOV    CL,BYTE PTR DS:[DI]    ;String length into CL
  177.     MOV    CH,00
  178.     MOV    SI,WORD PTR DS:[DI+2]    ;String offset into SI
  179.     MOV    DI,OFFSET FILE        ;ES:DI points to FILE
  180.     CLD
  181.     REPNZ    MOVSB            ;Store filename in FILE
  182.     RET                ;Return
  183. GET_FILENAME    ENDP
  184.  
  185. ;----------------------------------------------------------------------
  186. ;
  187. ;    Subroutine to set up new disk parameter table.
  188. ;
  189. ;    Passed: none
  190. ;    
  191. ;    Registers destroyed: none
  192. ;----------------------------------------------------------------------
  193. NEW_PARMS    PROC    NEAR
  194.     PUSH    DS            ;Save registers and segments
  195.     PUSH    SI
  196.     PUSH    DI
  197.     PUSH    CX
  198.     PUSH    AX
  199.     MOV    AX,00
  200.     MOV    DS,AX            ;Set DS = 00
  201.     CLI                ;Disable interrupts
  202.     LDS    SI,DS:[1EH*4]        ;Point to disk parameters
  203.     MOV    OFF_1E,SI        ;Save address of old disk parameters
  204.     MOV    SEG_1E,DS
  205.     MOV    DI,OFFSET DSKPRM    ;Place to store parameters
  206.     MOV    CX,0BH            ;Number of bytes in parameter table
  207.     CLD
  208.     REPNZ    MOVSB            ;Move them
  209.     MOV    AX,00
  210.     MOV    DS,AX            ;Set DS = 00
  211.     MOV    DI,OFFSET DSKPRM
  212.     MOV    WORD PTR DS:[1EH*4],DI    ;Point INT 1EH to DSKPRM
  213.     MOV    WORD PTR DS:[1EH*4+2],ES
  214.     MOV    BYTE PTR ES:[DI+3],03    ;Set disk parameters for 1024
  215.                     ;   bytes per sector
  216.     STI                ;Enable interrupts
  217.     POP    AX
  218.     POP    CX
  219.     POP    DI
  220.     POP    SI
  221.     POP    DS            ;Restore registers and segments
  222.     RET                ;Return
  223. NEW_PARMS    ENDP
  224.  
  225. ;----------------------------------------------------------------------
  226. ;
  227. ;    Subroutine to reset the diskette system.
  228. ;    If error is detected, ERROR% is set and the program is exited
  229. ;
  230. ;    Passed: none
  231. ;
  232. ;    Registers destroyed: none
  233. ;----------------------------------------------------------------------
  234. RESET    PROC    NEAR
  235.     PUSH    DX            ;Save registers
  236.     PUSH    CX
  237.     PUSH    BX
  238.     PUSH    AX
  239.     MOV    CX,00            ;Count tries
  240.  
  241. AGAIN:    INC    CX
  242.     CMP    CX,03            ;If more than three tries - quit
  243.     JA    ERR10
  244.     MOV    AX,00
  245.     INT    13H            ;Reset diskette system
  246.     CMP    AH,00            ;Was there an error ?
  247.     JNE    AGAIN            ;   Yes - retry
  248.  
  249.     POP    AX            ;Restore registers
  250.     POP    BX
  251.     POP    CX
  252.     POP    DX
  253.     RET                ;Return
  254.  
  255. ERR10:    POP    AX            ;Clear stack
  256.     POP    BX
  257.     POP    CX
  258.     POP    DX
  259.     MOV    AX,17            ;Error code = 17
  260.     POP    BX            ;Original return offset into BX
  261.     POP    DS            ;Restore DS
  262.     CALL    ERROR            ;Set error condition
  263.     JMP    DONE            ;Exit
  264. RESET    ENDP
  265.  
  266. ;----------------------------------------------------------------------
  267. ;
  268. ;    Subroutine restore the original disk parameters.
  269. ;
  270. ;    Passed: none
  271. ;
  272. ;    Registers destroyed: none
  273. ;----------------------------------------------------------------------
  274. RESTORE    PROC    NEAR
  275.     CMP    OFF_1E,00        ;Is offset = 00
  276.     JNE    OK21            ;   No  - needs to be switched
  277.     CMP    SEG_1E,00        ;Is segment = 00
  278.     JE    DONE2            ;   Yes - don't switch
  279.  
  280. OK21:    PUSH    DS
  281.     PUSH    AX
  282.     MOV    AX,00
  283.     MOV    DS,AX            ;Set DS to 0000
  284.     CLI                ;Disable interrupts while
  285.                     ;   changing interrupt vectors
  286.     MOV    AX,OFF_1E        ;Move offset for original INT 1EH
  287.     MOV    DS:[1EH*4],AX        ;   into interrupt table
  288.     MOV    AX,SEG_1E        ;Move segment for original INT 1EH
  289.     MOV    DS:[1EH*4+2],AX        ;   into interrupt table
  290.     STI                ;Enable interrupts
  291.     POP    AX
  292.     POP    DS
  293.  
  294. DONE2:    RET
  295. RESTORE    ENDP
  296.  
  297. ;----------------------------------------------------------------------
  298. ;
  299. ;    This subroutine is called from BASIC using:
  300. ;
  301. ;    CALL FMERGE(FILE$,ERROR%)
  302. ;
  303. ;    Passed:   FILE$ = Name of file to merge decoded sectors with.
  304. ;                      An ASCIIZ string with Drive, path, filename,
  305. ;                      and ending with a byte of zero.
  306. ;    Returned: ERROR%= Integer for the return of an error condition.
  307. ;                      Error codes are the same as described in the
  308. ;                      subroutine create.
  309. ;
  310. ;    by Keith M. Bailey         12/29/84        version 0.01
  311. ;
  312. ;----------------------------------------------------------------------
  313.     ASSUME CS:CODESG,DS:NOTHING,ES:DATASG
  314. FMERGE    PROC    FAR
  315.     PUSH    BP            ;Save BP
  316.     MOV    BP,SP
  317.     PUSH    ES            ;Save ES
  318.     MOV    AX,DATASG
  319.     MOV    ES,AX            ;Set ES = DATASG
  320.  
  321.     MOV    AX,00            ;Initialize with no error
  322.     CALL    ERROR
  323.  
  324.     CALL    GET_FILENAME        ;Filename in FILE
  325.  
  326.     PUSH    DS            ;Save DS
  327.     MOV    AX,DATASG
  328.     MOV    DS,AX            ;Set DS = DATASG
  329.     MOV    DX,OFFSET FILE        ;DS:DX points to filename
  330.     MOV    AX,3D02H        ;Open file for both reading
  331.     INT    21H            ;   and writing
  332.     JNC    OK30            ;If no error - continue
  333.     POP    DS            ;Restore DS
  334.     CALL    ERROR            ;Set error code
  335.     JMP    DONE3            ;Exit
  336.  
  337. OK30:    MOV    HANDLE,AX        ;Save file handle
  338.     MOV    BX,AX            ;File handle into BX
  339.  
  340. OK32:    MOV    AL,00            ;Move pointer to an offset
  341.     MOV    CX,00
  342.     MOV    DX,0010H        ;Pointer =  0000:0010
  343.     MOV    AH,42H
  344.     INT    21H            ;Advance file read/write pointer
  345.     JNC    OK33            ;If no error - continue
  346.     POP    DS            ;Restore DS
  347.     CALL    ERROR            ;Set error code
  348.     JMP    DONE3            ;Exit
  349.  
  350. OK33:    MOV    CX,0400H*9        ;Number of bytes to write
  351.     MOV    DX,OFFSET BUFFER    ;DS:DX points to data to write
  352.     MOV    AH,40H
  353.     INT    21H            ;Write to file
  354.     JNC    OK34            ;If no error - continue
  355.     POP    DS            ;Restore DS
  356.     CALL    ERROR            ;Set error code
  357.     JMP    DONE3            ;Exit
  358.  
  359. OK34:    CMP    AX,0400H*9        ;Correct number of bytes written ?
  360.     JE    OK35            ;   Yes - continue
  361.     POP    DS            ;Restore DS
  362.     MOV    AX,16
  363.     CALL    ERROR            ;Set error code
  364.     JMP    DONE3            ;Exit
  365.  
  366. OK35:    MOV    AH,3EH
  367.     INT    21H            ;Close file
  368.     POP    DS
  369.     JNC    DONE3            ;If no error - leave
  370.     CALL    ERROR            ;Set error code
  371.  
  372. DONE3:    POP    ES            ;Restore ES and BP
  373.     POP    BP
  374.     RET    4            ;Return
  375.  
  376. FMERGE    ENDP
  377.  
  378. CODESG    ENDS
  379.     END
  380.